<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Init extends CI_Controller {

	function __construct() {
        parent::__construct();
        $this->load->model('post_m');
    }
	function index()
	{
		echo 'Install permission default <br><br>';
		$this->per_default();
		
		echo 'Install Module default <br><br>';
		// $this->modules();
		
		echo 'Install Layout default <br><br>';
		$this->layout();

		echo 'Install Roles default <br><br>';
		// $this->roles();
		
	}
	
	function modules()
	{
		$modules = array();
		$modules[] = array(
			'module_name' =>'html_module',
			'module_title' =>'HTML/JS module',
			'module_config' =>'{}',
			);
		
		$modules[] = array(
			'module_name' =>'ultimate_posts_module',
			'module_title' =>'Bài viết',
			'module_config' =>'{}',
			);
			
		$modules[] = array(
			'module_name' =>'ads_module',
			'module_title' =>'Quảng cáo',
			'module_config' =>'{}',
			);
		$modules[] = array(
			'module_name' =>'category_module',
			'module_title' =>'Danh mục',
			'module_config' =>'{}',
			);
		$modules[] = array(
			'module_name' =>'layout_module',
			'module_title' =>'Layout',
			'module_config' =>'{"layout":"1","area":"header"}',
			);
		$modules[] = array(
			'module_name' =>'tab_posts_module',
			'module_title' =>'Tab bài viết',
			'module_config' =>'',
			);
		
		$this->db->truncate('modules');
		$this->db->insert_batch('modules',$modules);
	}
	
	function per_default()
	{
		$permissions = array();
		$groups = array();
		$groups[1] = array(
			'pgroup_title' => 'Quản lý tin',
			'pgroup_class' => 'fa-file-text-o',
			'pgroup_order' => 2,
		);
		$groups[2] = array(
			'pgroup_title' => 'Layout',
			'pgroup_class' => 'fa-th-large',
			'pgroup_order' => 6,
		);
		$groups[3] = array(
			'pgroup_title' => 'Thành viên',
			'pgroup_class' => 'fa-file-text-o',
			'pgroup_order' => 7,
		);
		$groups[4] = array(
			'pgroup_title' => 'Quản trị',
			'pgroup_class' => 'fa-file-text-o',
			'pgroup_order' => 8,
		);
		$groups[5] = array(
			'pgroup_title' => 'Công cụ',
			'pgroup_class' => 'fa-cogs',
			'pgroup_order' => 4,
		);
		$groups[6] = array(
			'pgroup_title' => 'Biên tập',
			'pgroup_class' => 'fa-file-text-o',
			'pgroup_order' => 1,
		);
		
		
		$groups[7] = array(
			'pgroup_title' => 'Dòng sự kiện',
			'pgroup_class' => 'fa-file-text-o',
			'pgroup_order' => 3,
		);
		
		$groups[8] = array(
			'pgroup_title' => 'Thống kê',
			'pgroup_class' => 'fa-file-text-o',
			'pgroup_order' => 5,
		);
		
		//Biên tập
		
		
		$permissions[] = array(
			'pgroup_id'=>6,
			'permission_name'=>'Posts.Create',
			'permission_title'=>'Tạo tin mới',
			'permission_description'=>'Tạo tin mới',
			'permission_is_sidebar'=> 1,
			'permission_sidebar_link'=> 'posts/create',
			'permission_sidebar_class'=> 'fa-pencil',
		);
		

		$permissions[] = array(
			'pgroup_id'=>6,
			'permission_name'=>'Posts.Sendlist',
			'permission_title'=>'Tin đã gửi',
			'permission_description'=>'Tin đã gửi',
			'permission_is_sidebar'=> 1,
			'permission_sidebar_link'=> 'posts/sendlist',
			'permission_sidebar_class'=> 'fa-edit',
		);
		$permissions[] = array(
			'pgroup_id'=>1,
			'permission_name'=>'Posts.Scheduledlist',
			'permission_title'=>'Tin hẹn đăng',
			'permission_description'=>'Tin hẹn đăng',
			'permission_is_sidebar'=> 1,
			'permission_sidebar_link'=> 'posts/scheduled',
			'permission_sidebar_class'=> 'fa-clock-o',
		);
		
		for($i=0; $i<8; $i++)
		{
			$info = $this->post_m->get_info_status($i);
			if($info)
			{
				foreach($info['permission'] as $key=>$perm)
				{
					$per = array();
					$per['pgroup_id'] = 1;
					$per['permission_name'] = $perm;
					$per['permission_sidebar_class'] = 'fa-file-text';
					$per['permission_is_sidebar'] = 0;
					$per['permission_sidebar_link'] = '';
					$title = $info['title'];
					if($i ==0)
					{
						$per['pgroup_id'] = 6;
					}
					if($key =='view')
					{
						$per['permission_sidebar_class'] = $this->post_m->get_post_status_icon($i);

						//tin bị trả lại
						if(in_array($i, array(4,5,6)))
							$per['permission_sidebar_class'] = 'fa-arrow-down';
						if(!$per['permission_sidebar_class'])
							
							$per['permission_sidebar_class'] = 'fa-file-text';
						$per['permission_is_sidebar'] = 1;
						$per['permission_sidebar_link'] = 'posts/index/'.$i;
					}

					if($key =='create')
					{
						$title ='Tạo '.mb_strtolower($info['title']);
					}

					if($key =='delete')
					{
						$title ='Xóa '.mb_strtolower($info['title']);
					}

					if($key =='edit')
					{
						$title ='Sửa '.mb_strtolower($info['title']);
					}

					$per['permission_title'] = $title;
					$per['permission_description'] = $per['permission_title'];
					
					
					$permissions[] = $per;
				}
				
			}
		}
		
		$permissions[] = array(
			'pgroup_id'=>1,
			'permission_name'=>'Term.Category.View',
			'permission_title'=>'Chuyên mục',
			'permission_description'=>'Quản lý chuyên mục',
			'permission_is_sidebar'=> 1,
			'permission_sidebar_link'=> 'term/index/category',
			'permission_sidebar_class'=> 'fa-file-text',
		);
		
		$permissions[] = array(
			'pgroup_id'=>2,
			'permission_name'=>'Layout.Default.Edit',
			'permission_title'=>'Layout mặc định',
			'permission_description'=>'Layout mặc định',
			'permission_is_sidebar'=> 1,
			'permission_sidebar_link'=> 'layout/index/1',
			'permission_sidebar_class'=> 'fa-th-large',
		);
		
		$permissions[] = array(
			'pgroup_id'=>2,
			'permission_name'=>'Layout.Home.Edit',
			'permission_title'=>'Layout trang chủ',
			'permission_description'=>'Layout trang chủ',
			'permission_is_sidebar'=> 1,
			'permission_sidebar_link'=> 'layout/index/2',
			'permission_sidebar_class'=> 'fa-th-large',
		);
		
		$permissions[] = array(
			'pgroup_id'=>2,
			'permission_name'=>'Layout.Cateparent.Edit',
			'permission_title'=>'Layout danh mục cha',
			'permission_description'=>'Layout danh mục cha',
			'permission_is_sidebar'=> 1,
			'permission_sidebar_link'=> 'layout/index/3',
			'permission_sidebar_class'=> 'fa-th-large',
		);
		
		$permissions[] = array(
			'pgroup_id'=>2,
			'permission_name'=>'Layout.CateSub.Edit',
			'permission_title'=>'Layout danh mục con',
			'permission_description'=>'Layout danh mục con',
			'permission_is_sidebar'=> 1,
			'permission_sidebar_link'=> 'layout/index/5',
			'permission_sidebar_class'=> 'fa-th-large',
		);
		
		$permissions[] = array(
			'pgroup_id'=>2,
			'permission_name'=>'Layout.Viewdetail.Edit',
			'permission_title'=>'Layout chi tiết bài viết',
			'permission_description'=>'Layout chi tiết bài viết',
			'permission_is_sidebar'=> 1,
			'permission_sidebar_link'=> 'layout/index/4',
			'permission_sidebar_class'=> 'fa-th-large',
		);

		$permissions[] = array(
			'pgroup_id'=>2,
			'permission_name'=>'Layout.Event.Edit',
			'permission_title'=>'Layout trang sự kiện',
			'permission_description'=>'Layout trang sự kiện',
			'permission_is_sidebar'=> 1,
			'permission_sidebar_link'=> 'layout/index/6',
			'permission_sidebar_class'=> 'fa-th-large',
		);
		
		$permissions[] = array(
			'pgroup_id'=>3,
			'permission_name'=>'Admin.View',
			'permission_title'=>'Admin',
			'permission_description'=>'Quản lý Admin',
			'permission_is_sidebar'=> 1,
			'permission_sidebar_link'=> 'admin',
			'permission_sidebar_class'=> 'fa-user',
		);
		$permissions[] = array(
			'pgroup_id'=>3,
			'permission_name'=>'Admin.Create',
			'permission_title'=>'Thêm mới Admin',
			'permission_description'=>'Thêm mới Admin',
			'permission_is_sidebar'=> 0,
			'permission_sidebar_link'=> '',
			'permission_sidebar_class'=> '',
		);
		$permissions[] = array(
			'pgroup_id'=>3,
			'permission_name'=>'Admin.Edit',
			'permission_title'=>'Cập nhật thông tin Admin',
			'permission_description'=>'Cập nhật thông tin Admin',
			'permission_is_sidebar'=> 0,
			'permission_sidebar_link'=> '',
			'permission_sidebar_class'=> '',
		);
		$permissions[] = array(
			'pgroup_id'=>3,
			'permission_name'=>'Admin.Delete',
			'permission_title'=>'Xóa Admin',
			'permission_description'=>'Xóa Admin',
			'permission_is_sidebar'=> 0,
			'permission_sidebar_link'=> '',
			'permission_sidebar_class'=> '',
		);
		
		$permissions[] = array(
			'pgroup_id'=>3,
			'permission_name'=>'Roles.View',
			'permission_title'=>'Nhóm',
			'permission_description'=>'Quản lý Nhóm',
			'permission_is_sidebar'=> 1,
			'permission_sidebar_link'=> 'roles',
			'permission_sidebar_class'=> 'fa-group',
		);
		
		$permissions[] = array(
			'pgroup_id'=>3,
			'permission_name'=>'Roles.Create',
			'permission_title'=>'Thêm nhóm',
			'permission_description'=>'Thêm nhóm',
			'permission_is_sidebar'=>0,
			'permission_sidebar_link'=> '',
			'permission_sidebar_class'=> '',
		);
		
		$permissions[] = array(
			'pgroup_id'=>3,
			'permission_name'=>'Roles.Edit',
			'permission_title'=>'Sửa nhóm',
			'permission_description'=>'Sửa nhóm',
			'permission_is_sidebar'=>0,
			'permission_sidebar_link'=> '',
			'permission_sidebar_class'=> '',
		);
		
		$permissions[] = array(
			'pgroup_id'=>3,
			'permission_name'=>'Roles.Delete',
			'permission_title'=>'Xóa nhóm',
			'permission_description'=>'Xóa nhóm',
			'permission_is_sidebar'=>0,
			'permission_sidebar_link'=> '',
			'permission_sidebar_class'=> '',
		);
		
		$permissions[] = array(
			'pgroup_id'=>3,
			'permission_name'=>'Permissions.View',
			'permission_title'=>'Quyền',
			'permission_description'=>'Quản lý Quyền',
			'permission_is_sidebar'=> 1,
			'permission_sidebar_link'=> 'permissions',
			'permission_sidebar_class'=> 'fa-file-text',
		);
		
		$permissions[] = array(
			'pgroup_id'=>3,
			'permission_name'=>'Permissions.Create',
			'permission_title'=>'Thêm quyền',
			'permission_description'=>'Thêm quyền',
			'permission_is_sidebar'=>0,
			'permission_sidebar_link'=> '',
			'permission_sidebar_class'=> '',
		);
		
		$permissions[] = array(
			'pgroup_id'=>3,
			'permission_name'=>'Permissions.Edit',
			'permission_title'=>'Sửa quyền',
			'permission_description'=>'Sửa quyền',
			'permission_is_sidebar'=>0,
			'permission_sidebar_link'=> '',
			'permission_sidebar_class'=> '',
		);
		
		$permissions[] = array(
			'pgroup_id'=>3,
			'permission_name'=>'Permissions.Delete',
			'permission_title'=>'Xóa quyền',
			'permission_description'=>'Xóa quyền',
			'permission_is_sidebar'=>0,
			'permission_sidebar_link'=> '',
			'permission_sidebar_class'=> '',
		);
		
		$permissions[] = array(
			'pgroup_id'=>4,
			'permission_name'=>'Ads.View',
			'permission_title'=>'Quảng cáo',
			'permission_description'=>'Quản lý Quảng cáo',
			'permission_is_sidebar'=> 1,
			'permission_sidebar_link'=> 'ads',
			'permission_sidebar_class'=> 'fa-file-text',
		);

		$permissions[] = array(
			'pgroup_id'=>4,
			'permission_name'=>'Ads.Edit',
			'permission_title'=>'Sửa Quảng cáo',
			'permission_description'=>'Sửa Quảng cáo',
			'permission_is_sidebar'=> 0,
			'permission_sidebar_link'=> '',
			'permission_sidebar_class'=> '',
		);
		
		$permissions[] = array(
			'pgroup_id'=>4,
			'permission_name'=>'Setting.View',
			'permission_title'=>'Cấu hình',
			'permission_description'=>'Quản lý Cấu hình',
			'permission_is_sidebar'=> 1,
			'permission_sidebar_link'=> 'setting',
			'permission_sidebar_class'=> 'fa-cog',
		);
		
		$permissions[] = array(
			'pgroup_id'=>7,
			'permission_name'=>'Term.Event.View',
			'permission_title'=>'Sự kiện',
			'permission_description'=>'Sự kiện',
			'permission_is_sidebar'=> 1,
			'permission_sidebar_link'=> 'term/event',
			'permission_sidebar_class'=> 'fa-link',
		);
		
		$permissions[] = array(
			'pgroup_id'=>7,
			'permission_name'=>'Term.Event.Edit',
			'permission_title'=>'Sửa sự kiện',
			'permission_description'=>'Sửa sự kiện',
			'permission_is_sidebar'=> 0,
			'permission_sidebar_link'=> '',
			'permission_sidebar_class'=> 'fa-link',
		);
		
		$permissions[] = array(
			'pgroup_id'=>7,
			'permission_name'=>'Term.Event.Create',
			'permission_title'=>'Tạo sự kiện',
			'permission_description'=>'Tạo sự kiện',
			'permission_is_sidebar'=> 0,
			'permission_sidebar_link'=> '',
			'permission_sidebar_class'=> 'fa-link',
		);

		$permissions[] = array(
			'pgroup_id'=>7,
			'permission_name'=>'Term.Event.Delete',
			'permission_title'=>'Xóa sự kiện',
			'permission_description'=>'Xóa sự kiện',
			'permission_is_sidebar'=> 0,
			'permission_sidebar_link'=> '',
			'permission_sidebar_class'=> 'fa-link',
		);
		
		$this->db->truncate('group_permissions');
		$this->db->truncate('permissions');
		$this->db->truncate('role_permissions');
		$this->db->insert_batch('group_permissions',$groups);
		
		foreach($permissions as $permission)
		{
			$this->db->insert('permissions',$permission);
			$id = $this->db->insert_id();
			$this->db->insert('role_permissions',array(
			 'role_id' =>1,
			 'permission_id' =>$id,
			));
		}
		
	}
	
	function layout()
	{
		$layouts = array();
		$layouts[] = array(
			'layout_id' => 1,
			'file_name' => 'default',
			'layout_name' => 'Layout mặc định',
			'layout_uri' => '',
			'can_delete' => 0,
		);
		$layouts[] = array(
			'layout_id' => 2,
			'file_name' => 'home',
			'layout_name' => 'Layout trang chủ',
			'layout_uri' => '',
			'can_delete' => 0,
		);
		$layouts[] = array(
			'layout_id' => 3,
			'file_name' => 'cateparent',
			'layout_name' => 'Layout danh mục cha',
			'layout_uri' => '',
			'can_delete' => 0,
		);
		$layouts[] = array(
			'layout_id' => 4,
			'file_name' => 'viewdetail',
			'layout_name' => 'Layout trang bài viết',
			'layout_uri' => '',
			'can_delete' => 0,
		);
		$layouts[] = array(
			'layout_id' => 5,
			'file_name' => 'catesub',
			'layout_name' => 'Layout danh mục con',
			'layout_uri' => '',
			'can_delete' => 0,
		);
		$layouts[] = array(
			'layout_id' => 6,
			'file_name' => 'event',
			'layout_name' => 'Layout trang sự kiện',
			'layout_uri' => '',
			'can_delete' => 0,
		);
		
		$this->db->truncate('layouts');
		$this->db->insert_batch('layouts',$layouts);
	}

	function roles()
	{
		$roles = array();
		$roles[] = array('role_name'=>'Administrator');
		$roles[] = array('role_name'=>'Quản trị');
		$roles[] = array('role_name'=>'Biên tập viên');
		$roles[] = array('role_name'=>'Người viết bài');
		$this->db->truncate('roles');
		$this->db->insert_batch('roles',$roles);
	}
}